package com.partner.action.sysbase;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.partner.bean.sysright.TSysrole;
import com.partner.bean.sysright.TSysuser;
import com.partner.bean.sysright.TSysuserVo;
import com.partner.frame.BaseAction;
import com.partner.frame.Result;
import com.partner.frame.common.ConstantsMark;
import com.partner.frame.support.util.TujiPassWordEncoder;
import com.partner.service.sysright.SysManagerService;
import com.partner.service.sysright.SysRoleManagerService;

@RestController
public class SystemSettingAction extends BaseAction {
	
	@Autowired
	private SysManagerService sysManagerService;
	@Autowired
	private SysRoleManagerService sysRoleManagerService;
	
	/**
	 * 进入管理员列表
	 */
	@RequestMapping(value="/systemSetting/sysmanagerlist",method=RequestMethod.GET)
	public ModelAndView sysManagerList(ModelAndView mv,HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		List<TSysuser> list = sysManagerService.getList();
        mv.addObject("rows", list);
        mv.setViewName("/pages/sysright/sysmanagerlist");

		return mv;
		
	}
	/**
	 * 进入编辑管理员
	 */
	@RequestMapping(value="/systemSetting/editSysManager",method=RequestMethod.GET)
	public ModelAndView editSysManager(TSysuser bean,ModelAndView mv,HttpServletRequest request,HttpServletResponse respose) throws Exception {
		
		TSysuser user =  null ; 
		if(bean.getId()>0){
			user = sysManagerService.selectByPrimaryKey(bean.getId());
			mv.addObject("user", user);
			mv.addObject("operate", "编辑");
		}else{
			mv.addObject("operate", "新增");
		}
        List<TSysrole> data = sysRoleManagerService.getAllRoles();
		mv.addObject("rolerows", data);
        mv.setViewName("/pages/sysright/sysmanageradd");

		return mv;
		
	}
	
	/**
	 * 更新管理员信息
	 */
	@RequestMapping(value="/systemSetting/updateSysManager",method=RequestMethod.POST)
	public Result updateSysManager(TSysuser bean,ModelAndView mv,HttpServletRequest request,HttpServletResponse response) throws Exception {
		Result rs = new Result();
		//Md5PasswordEncoder.Md5PasswordEncoder();
		//Md5PasswordEncoder t = new Md5PasswordEncoder();
		//passwordEncoder.encodePassword(bean.getPassword(), "");
		//t.encodePassword(bean.getPassword(), "");
		
		String md5pwd = "" ;
		if(bean.getId()==null){
			TSysuser user =  sysManagerService.getSysUserbyUsername(bean);
			if(user!=null){
				rs.setDesc(ConstantsMark.ACCOUNT_EXSIT);
				rs.setStatus(ConstantsMark.ACCOUNT_EXSITCODE);
				return rs ;
			}
			if(bean.getPassword()==null||bean.getPassword().equals("")){
				md5pwd = TujiPassWordEncoder.MD5("123456");//MD5加密
			}else{
				md5pwd = TujiPassWordEncoder.MD5(bean.getPassword());//MD5加密
			}
			bean.setPassword(md5pwd);
			sysManagerService.insert(bean);
		}else{
			sysManagerService.updateByPrimaryKey(bean);
		}
		rs.setDesc(ConstantsMark.INFO_UPDATESUCCESS);
		rs.setStatus(ConstantsMark.SUCCESS);
		
		return rs ; 
	}
	/**
	 * 删除管理员
	 */
	@RequestMapping(value="/systemSetting/deleteSysManager",method=RequestMethod.POST)
	public Result deleteSysManager(String ids) throws Exception {
		
		Result rs = new Result();
		String[] arr = ids.split(",");
		for(String i : arr){
			if(i.equals(""))continue;
			sysManagerService.deleteByPrimaryKey(Integer.valueOf(i));
		}
		rs.setDesc(ConstantsMark.INFO_DELETESUCCESS);
		rs.setStatus(ConstantsMark.SUCCESS);
		
		return rs ; 
	}
	
	/**
	 * 调整管理员状态
	 */
	@RequestMapping(value="/systemSetting/setSysManagerStatu",method=RequestMethod.POST)
	public Result setSysManagerStatu(TSysuser bean) throws Exception {
		
		Result rs = new Result();
		if(bean!=null&&bean.getId()!=null){
			sysManagerService.updateByPrimaryKey(bean);
		}
		rs.setDesc(ConstantsMark.INFO_SETSUCCESS);
		rs.setStatus(ConstantsMark.SUCCESS);
		
		return rs ; 
	}
	/**
	 * 查询管理员
	 */
	@RequestMapping(value="/systemSetting/searchSysManager",method=RequestMethod.GET)
	public ModelAndView searchSysManager(TSysuserVo bean,ModelAndView mv,HttpServletRequest request,HttpServletResponse respose) throws Exception {
		
		List<TSysuser> list = sysManagerService.getListByPara(bean);
        mv.addObject("rows", list);
        mv.addObject("user", bean);
        mv.setViewName("/pages/sysright/sysmanagerlist");

		return mv;
		
	}

}
